package com.by.datasource.dynamictablename.algorithm;

import com.by.datasource.dynamictablename.algorithm.annotation.DyTNAlgorithm;
import com.by.datasource.dynamictablename.consts.DyTNConst;
import com.by.toolkit.platform.ByContext;
import com.by.toolkit.utils.SplitterUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
@DyTNAlgorithm(DyTNConst.PROJECT_ID)
public class ProjectIdAlgorithm implements AbstractDyTNAlgorithm {

    @Override
    public String dynamic(String tableName) {
        Long projectId = ByContext.getProjectId();
        log.info("[ProjectIdAlgorithm][dynamic] tableName:[{}] projectId:[{}]", tableName, projectId);
        if (Objects.isNull(projectId)) {
            return tableName;
        }
        return SplitterUtil.UNDERSCORE_JOINER.join(tableName, projectId);
    }

}
